<?php
namespace Wap\Controller;
use Think\Controller;
use Wap\Service\UUIDService;
use Common\Service\Sms\ShortMessageService;

class ZJDailyCardCustomerController extends Controller
{
    public function login()
    {
        $result = array( "error" => 404, "msg" => "登录信息错误！" );
		$loginTelephone = I( "loginTelephone", "", "" );
		$loginPassword = I( "loginPassword", "", "" );

		if( empty( $loginPassword ) || empty( $loginTelephone ) ){//TODO: 验证手机号码格式
			$result['msg'] = "用户名或密码不能为空";
			return $this->ajaxReturn( $result );
		}

		$mode	=	M( "zjdailycard_customer" );
        $condition = array( "telephone" => $loginTelephone, "password" => md5( $loginPassword ) );
		$exist = $mode->where( $condition )->getField("id");
		if( $exist != null ){
            //更新当前用户id
            session( "zjdailyCardCustomer.login", $exist );
            session( "zjdailyCardCustomer.loginTime", time() );

			$result['error'] = 200;
			$result['customer'] = $exist;
			$result['msg']	= "登录成功";
		}else{
			$result['msg'] = "用户名或密码错误";
		}
		return $this->ajaxReturn( $result );

    }

    public function registe()
    {
        $result = array( "error" => 404, "msg" => "registe", "validate" => array() );

		$registeTelephone = trim( I( "post.registeTelephone", "", "" ) );
		$registePassword = trim( I( "post.registePassword", "", "" ) );
		$registeSmscode = trim( I( "post.registeSmscode", "", "" ) );
		$registeName = trim( I( "post.registeName", "", "" ) );

		//判断手机号码格式是否正确、密码长度
		if( preg_match( "/^1\d{10}$/", $registeTelephone ) !== 1 )
		{
			$result['msg'] = "手机号码格式错误";
			return $this->ajaxReturn( $result );
		}

        if( preg_match( "/[^a-zA-Z0-9]/", $registePassword ) || strlen( $registePassword )  > 12 || strlen( $registePassword ) < 6 )
        {
            $result['msg'] = "密码错误,只能由6-12位字母和数字组成";
            return $this->ajaxReturn( $result );
        }

        if( !preg_match( "/^[\x{4E00}-\x{9FA5}]+$/u", $registeName ) )//param should be encoded by utf-8, or cause error
        {
            $result['msg'] = "用户名不能包含特殊字符";
            return $this->ajaxReturn( $result );
        }

        if( mb_strlen($registeName) < 3 || mb_strlen( $registeName ) > 20 ) //mb_strlen just used for unicode \x{4e00} ~ \x{9fa5}
        {
            $result['msg'] = "用户名为2-20位中文";
            return $this->ajaxReturn( $result );
        }

		//判断手机号码有没有注册过
		$mode	=	D( "zjdailycard_customer" );
        $condition = array( "telephone" => $registeTelephone );
		$exist	=	$mode->where( $condition )->find();
		if( $exist ){
			$result['msg']	=	"号码已经注册过了";
			return $this->ajaxReturn( $result );
		}

		//判断短信验证码是否正确
		//$sms = SessionService::get_smscode( $registeTelephone, 60 );
        $s_smscode = session( "zjdailyCardCustomer.registeSmscode" );
        $s_time = session( "zjdailyCardCustomer.registeTime" );
        $expire = 1*60;
        $time = time();

		if( !$s_smscode || ($s_smscode && ($time - intval( $s_time ) > $expire)) || $s_smscode != $registeSmscode ){
			$result['msg']	=	"验证码信息有误";
			return $this->ajaxReturn( $result );//
		}

		//执行注册
		$customer = array( "username" => $registeName,
            "telephone" => $registeTelephone,
            "password" => md5($registePassword),
            "create_time" => time(),
            'updated_time' => time(),
            'login_time' => time(),
            'logined_ip' => getenv( "HTTP_CLIENT_IP" ),
            'logining_ip' => getenv( "HTTP_CLIENT_IP" ) );
        $registed = M( "zjdailycard_customer" )->add( $customer );
		if( !$registed ){
			$result['msg'] = "注册失败";
			return $this->ajaxReturn( $result );
		}else{
            //CustomerService::setID( $saved['data']['id'] );
            //更新当前用户id
            session( "zjdailyCardCustomer.login", $registed );
            session( "zjdailyCardCustomer.loginTime", time() );
            session( "zjdailyCardCustomer.registeTelephone", null );
            session( "zjdailyCardCustomer.registeSmscode", null );
            session( "zjdailyCardCustomer.registeTime", null );

            $result['error'] = 200;
            $result['msg'] = "注册成功";
			$result['customer'] = $registed;
			return $this->ajaxReturn( $result );
		}
    }

    public function checkIsOnline()
    {
        $result = array( "error" => 404, "msg" => "用户不在线" );
        $exist = session( "zjdailyCardCustomer.login" );
        if( $exist ){
            $result['error'] = 200;
            $result['msg'] = "用户在线";
        }
        $this->ajaxReturn( $result );
    }

    public function forgetPassword()
    {
        $telephone = I( "post.telephone", "", "" );
        $password = I( "post.password", "", "" );
        $smscode = I( "post.smscode", "", "" );

        $s_smscode = session( "zjdailyCardCustomer.forgetSmscode" );
        $s_telephone = session( "zjdailyCardCustomer.forgetTelephone" );
        $s_time = session( "zjdailyCardCustomer.forgetTime" );
        $time = time();
        $expire = 1*60;

        $result = array( "error" => "404", 'msg' => "手机号码或验证码不能为空" );
        if( empty( $telephone ) ){
            $result['msg'] = "手机号码不能为空";
        }

        $exist = M( "zjdailycard_customer" )->where( array( "telephone" => $telephone ) )->find();
        if( !$exist ){
            $result['msg'] = "用户不存在";
            $this->ajaxReturn( $result );
        }

        if( empty( $smscode ) )
        {
            $result['msg'] = "短信验证码不能为空";
            $this->ajaxReturn( $result );
        }

        if( empty($password) ){
            $result['msg'] = "密码格式不正确";
            $this->ajaxReturn( $result );
        }

        if( $time - $s_time > $expire )
        {
            $result['msg'] = "验证码已经过期";
            $this->ajaxReturn( $result );
        }

        if( $s_smscode != $smscode  ){
             $result['msg'] = "验证码不正确";
             $this->ajaxReturn( $result );
        }

        //session( "zjdailyCardCustomer.forgetTime", null );
        //session( "zjdailyCardCustomer.forgetTelephone", null );
        //session( "zjdailyCardCustomer.forgetSmscode", null );

        $updated = M( "zjdailycard_customer" )->where( array( "telephone" => $telephone ) )->setField( array( "password"  => md5( $password ) ) );
        if( $updated === false ){
            $result['msg'] = "重置失败，请重试";
            $this->ajaxReturn( $result );
        }else{
            $result['error'] = 200;
            $result['msg'] = "重置成功，请登录";
            $this->ajaxReturn( $result );
        }
    }

    public function smscodeForRegiste()
    {
        $telephone = I( "post.telephone", "", "" );

        $s_telephone = session( "zjdailyCardCustomer.registeTelephone" );
        $s_smscode = session( "zjdailyCardCustomer.registeSmscode" );
        $s_time = session( "zjdailyCardCustomer.registeTime" );
        $time = time();
        $expire = 1*60;

        $result = array( "error" => 404, "msg" => "操作发生错误" );
        if( empty( $telephone ) ){
            $result['msg'] = "手机号码不能为空";
            $this->ajaxReturn( $result );
        }

        $exist = M( "zjdailycard_customer" )->where( array( "telephone" => $telephone ) )->find();
        if( $exist ){
            $result['msg'] = "用户已经注册过了";
            $this->ajaxReturn( $result );
        }

        if( $s_smscode && ( $time - intval($s_time) ) < $expire ){
            $result['error'] = 304;
            $result['msg'] = "验证码未失效";
            $this->ajaxReturn( $result );
        }else{
            $s_time = $time;

			$s_smscode = UUIDService::generate_num( 6 );
            $msg = sprintf( "浙江日报报卡激活系统提示: 注册验证码 %d", $s_smscode );
			$sms = new ShortMessageService();
        	$sended =  $sms->sendMsgTo( $telephone, $msg );
            $sended = true;

            if( $sended ){
                session( "zjdailyCardCustomer.registeTelephone", $telephone );
                session( "zjdailyCardCustomer.registeSmscode", $s_smscode );
                session( "zjdailyCardCustomer.registeTime", $s_time );

                $result['sms'] = $s_smscode;
                $result['session'] = $_SESSION;
                $result['error'] = 200;
                $result['msg'] = "短信验证码发送成功$s_smscode";
            }else{
                $result['error'] = 204;
                $result['msg'] = "短信验证码发送失败";
            }
            $this->ajaxReturn( $result );
        }
    }

    public function smscodeForForget()
    {
        $telephone = I( "post.telephone", "", "" );

        $s_telephone = session( "zjdailyCardCustomer.forgetTelephone" );
        $s_smscode = session( "zjdailyCardCustomer.forgetSmscode" );
        $s_time = session( "zjdailyCardCustomer.forgetTime" );
        $time = time();
        $expire = 1*60;

        $result = array( "error" => 404, "msg" => "操作发生错误" );
        if( empty( $telephone ) ){
            $result['msg'] = "手机号码不能为空";
            $this->ajaxReturn( $result );
        }

        $exist = M( "zjdailycard_customer" )->where( array( "telephone" => $telephone ) )->find();
        if( !$exist ){
            $result['msg'] = "用户不存在";
            $this->ajaxReturn( $result );
        }

        if( $s_smscode && ( $time - intval($s_time) ) < $expire ){
            $result['error'] = 304;
            $result['msg'] = "验证码未失效";
            $this->ajaxReturn( $result );
        }else{
            $s_time = $time;

			$s_smscode = UUIDService::generate_num( 6 );
            $msg = sprintf( "浙江日报报卡激活系统提示: 重置验证码 %d", $s_smscode );
			$sms = new ShortMessageService();
        	$sended =  $sms->sendMsgTo( $telephone, $msg );

            // $sended = true;
            if( $sended ){
                session( "zjdailyCardCustomer.forgetTelephone", $telephone );
                session( "zjdailyCardCustomer.forgetSmscode", $s_smscode );
                session( "zjdailyCardCustomer.forgetTime", $s_time );

                $result['error'] = 200;
                $result['smscode'] = $s_smscode;
                $result['msg'] = "短信验证码发送成功";
            }else{
                $result['error'] = 204;
                $result['msg'] = "短信验证码发送失败";
            }
            $this->ajaxReturn( $result );
        }
    }
}
?>
